package org.proddevplm.business.user;

import java.util.List;
import java.util.Map;

import org.proddevplm.business.api.AController;
import org.proddevplm.business.user.constants.UserMgmtConstants;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.constants.UserConstants;
import org.proddevplm.dao.data.Product;
import org.proddevplm.dao.data.Project;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;

public class UserController extends AController {

	public Object execute(int command, Object data) {

		Object returnObject = null;
		int projectId;
		int userId;
		int roleId;

		switch (command) {
		case UserMgmtConstants.ADD_USER_COMMAND:
			List<String> adaptedData = (List<String>)data;
			for (int i=0; i<adaptedData.size();i++){
				if ("".equals(adaptedData.get(i))) {
					putOnState(UserConstants.ERROR_MESSAGE, "Please fill in all the fields");
					putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
					return null;
				}
			}
			if(!adaptedData.get(3).equals(adaptedData.get(4))) {
				putOnState(UserConstants.ERROR_MESSAGE, "Please check password");
				putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
				return null;
			}
			else if (getDaoManager().getUserManager().getUsersByUsername(adaptedData.get(2))){
				putOnState(UserConstants.ERROR_MESSAGE, "Please choose different username");
				putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
				return null;
			}
			else{
				User user = new User();
				user.setSurname(adaptedData.get(0));
				user.setName(adaptedData.get(1));
				
				user.setUsername(adaptedData.get(2));
				user.setPassword(adaptedData.get(3));
				user.setEmail(adaptedData.get(5));
				user.setType(adaptedData.get(6));
				user.setCompany(adaptedData.get(7));
				user.setPosition(adaptedData.get(8));
				user.setCategory("devTeam");
				
				putOnState(ControllerConstants.NEXT_PAGE, "startPage.jsp");
				putOnState(UserConstants.NOTICE_MESSAGE, "You have successfully signed-up. Please login");
				
				returnObject = getDaoManager().getUserManager().addUser(user);
			}
			
			//returnObject = manager.addUser((User) data);
			break;
		case UserMgmtConstants.DELETE_USER_COMMAND:
			returnObject = getDaoManager().getUserManager().removeUser((Integer) data);
			break;
		case UserMgmtConstants.GET_USER_COMMAND:
			returnObject = getDaoManager().getUserManager().getUser((Integer) data);
			break;
		case UserMgmtConstants.UPDATE_USER_COMMAND:
			Map<String, Object> dataMap = (Map<String, Object>) data;
			returnObject = getDaoManager().getUserManager().updateUser(
					(Integer) dataMap.get(UserMgmtConstants.USER_ID_FIELD),
					(User) dataMap.get(UserMgmtConstants.USER_DATA_FIELD));
			break;
		case UserMgmtConstants.GET_USERS_COMMAND:
			returnObject = getDaoManager().getUserManager().getUsers();
			break;
		case UserMgmtConstants.LOGIN_COMMAND:
			String credentials[] = (String[])data;
			User user = getDaoManager().getUserManager().login(credentials[0], credentials[1]);
			if (user == null){
				putOnState(UserConstants.ERROR_MESSAGE, "Invalid username and password");
				putOnState(ControllerConstants.NEXT_PAGE, "startPage.jsp");
			}else {
				if(user.getCategory().equals("devTeam")){
					getSession().setAttribute(UserConstants.USER, user);
					putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectIndex.jsp");
				}
				else{
					List<Product> allProducts = getDaoManager().getProductManager().getAllProducts();
					putOnSession("allProducts", allProducts);
					getSession().setAttribute(UserConstants.USER, user);
					putOnState(ControllerConstants.NEXT_PAGE, "content/lifeCycleData/usePhase/consumerPage.jsp");
				}
			}
			break;
			
		case UserMgmtConstants.LOGOUT_COMMAND:
			getSession().invalidate();
			putOnState(ControllerConstants.NEXT_PAGE, "startPage.jsp");
			break;
		
		case UserMgmtConstants.ADD_MEMBER_ON_PROJECT:
			Map<String, Object> newMemberMap = (Map<String, Object>) data;
			projectId = (Integer)getSession().getAttribute("projectId");
			Project currentProject =  getDaoManager().getProjectManager().getProjectByProjectId(projectId);
			userId = Integer.valueOf((String)newMemberMap.get("userId"));
			User currentUser = (User)getDaoManager().getUserManager().getUser(userId);
			roleId = Integer.valueOf((String)newMemberMap.get("roleId"));
			Roles role = (Roles)getDaoManager().getRoleManager().getRoleById(roleId);
			getDaoManager().getUserManager().addMemberOnProject(currentProject, currentUser, role);
			break;
			
		case UserMgmtConstants.REMOVE_MEMBER_FROM_PROJECT:
			Map<String, Object> remMemberMap = (Map<String, Object>) data;
			projectId = Integer.valueOf((String)remMemberMap.get("projectId"));
			userId = Integer.valueOf((String)remMemberMap.get("userId"));
			roleId = Integer.valueOf((String)remMemberMap.get("roleId"));
			getDaoManager().getUserManager().removeUserFromProject(projectId, userId, roleId);
			break;
			
		case UserMgmtConstants.CHANGE_ROLE:
			Map<String, Object> changeRoleMap = (Map<String, Object>)data;
			projectId = Integer.valueOf((String)changeRoleMap.get("projectId"));
			userId = Integer.valueOf((String)changeRoleMap.get("userId"));
			roleId = Integer.valueOf((String)changeRoleMap.get("currentRoleId"));
			
			RelationMatrix currentRelMatrix = getDaoManager().getProjectManager().getRelMatrixByProjUserRole(projectId, userId, roleId);
			
			int newRoleId = Integer.valueOf((String)changeRoleMap.get("newRoleId"));
			Roles newRole = getDaoManager().getRoleManager().getRoleById(newRoleId);
			getDaoManager().getProjectManager().changeRelMatrixByRole(currentRelMatrix, newRole);
			break;
			
		case UserMgmtConstants.ADD_GUEST:
			List<String> adaptedGuestData = (List<String>)data;
			for (int i=0; i<adaptedGuestData.size();i++){
				if ("".equals(adaptedGuestData.get(i))) {
					putOnState(UserConstants.ERROR_MESSAGE, "Please fill in all the fields");
					putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
					return null;
				}
			}
			if(!adaptedGuestData.get(3).equals(adaptedGuestData.get(4))) {
				putOnState(UserConstants.ERROR_MESSAGE, "Please check password");
				putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
				return null;
			}
			else if (getDaoManager().getUserManager().getUsersByUsername(adaptedGuestData.get(2))){
				putOnState(UserConstants.ERROR_MESSAGE, "Please choose different username");
				putOnState(ControllerConstants.NEXT_PAGE, "signUp.jsp");
				return null;
			}
			else{
				User guest = new User();
				guest.setSurname(adaptedGuestData.get(0));
				guest.setName(adaptedGuestData.get(1));
				guest.setUsername(adaptedGuestData.get(2));
				guest.setPassword(adaptedGuestData.get(3));
				guest.setEmail(adaptedGuestData.get(5));
				guest.setCategory("guest");
								
				putOnState(ControllerConstants.NEXT_PAGE, "startPage.jsp");
				putOnState(UserConstants.NOTICE_MESSAGE, "You have successfully signed-up. Please login");
				
				returnObject = getDaoManager().getUserManager().addUser(guest);
			}
			break;
		}

		return returnObject;
	}

}